import {fetchTrainStation, fetchAirPortStation} from "../../api/dailyLife";
const parseStation = (stations) => {
    // chinese: "阿巴嘎旗"
    // code: "AQC"
    // hot: 1
    // jiangIf: 2
    // pingyin: "abagaqi"
    // uyghur: "ئاباگاچى"
    //
    //
    // airportCode: "AAF"
    // airportName: "阿巴拉契克拉地方机场"
    // airportNameUyghur: "阿巴拉契克拉地方机场"
    // cityCode: "AAF"
    // cityName: "阿巴拉契克拉"
    // cityNameUyghur: "阿巴拉契克拉"
    // cityPinYin: "abalaqikela"
    // hot: 2
    // jiangIf: 1
    const {chinese, uyghur, pingyin, code} = stations
    return {
        chinese,
        uyghur,
        pingyin,
        code,
        name: chinese,
        letter: pingyin[0].toUpperCase()
    }
}

const parseAirPort = (stations) => {
    return {
        ...stations,
        chinese: stations.airportName,
        uyghur: stations.airportNameUyghur,
        pingyin: stations.cityPinYin,
        code: stations.airportCode,
        name: stations.airportName,
        letter: stations.cityPinYin.slice(0,1).toUpperCase()
    }
}
export default {
    namespaced: true,
    state: {
        // 搜索关键字
        trainStation: {
            jiang: [],
            hot: [],
            all: []
        },
		airStation: {
            jiang: [],
            hot: [],
            all: []
        },
		stationType: ''
    },
    getters: {
        indexed ({trainStation, airStation, stationType }) {
			if (stationType === 'train') {
				const {all = []} = trainStation
				const letters = [...new Set(all.map(item => item.letter))]
				letters.sort()
				return letters.map(letter => {
				    const data = all.filter(station => station.letter === letter)
				    return {
				        letter,
				        data
				    }
				})
			} else {
				const {all = []} = trainStation
				const letters = [...new Set(all.map(item => item.letter))]
				letters.sort()
				return letters.map(letter => {
				    const data = all.filter(station => station.letter === letter)
				    return {
				        letter,
				        data
				    }
				})
			}
            
        },
        hotCityList ({trainStation, airStation, stationType}) {
			if (stationType === 'train') { 
				const {hot = []} = trainStation
				return hot
			} else {
				const {hot = []} = airStation
				return hot
			}
           
        },
        jiang ({trainStation, airStation, stationType}) {
			if (stationType === 'train') {
				const {jiang = []} = trainStation
				return jiang
			} else {
				const {jiang = []} = airStation
				return jiang
			}
        },
    },
    mutations: {
        setTrainStation (state, trainStation) {
            const {jiang = [], hot = [], all = []} = trainStation
            state.trainStation = {
                jiang: jiang.map(parseStation),
                hot: hot.map(parseStation),
                all: all.map(parseStation)
            }
        },
        setAirPort(state, trainStation) {
            const {jiang = [], hot = [], all = []} = trainStation
            state.airStation = {
                jiang: jiang.map(parseAirPort),
                hot: hot.map(parseAirPort),
                all: all.map(parseAirPort)
            }
        },
		setStationType(state, type) {
			state.stationType = type
		}
    },
    actions: {
        getTrainStation ({state, commit}, type) {
			commit('setStationType', type)
            const {trainStation, airStation} = state
            const data = {
                userId: this.state.app.uid
            }
            console.log('type', type)
            if (type === 'train') {
				if (Object.keys(trainStation).some(e => trainStation[e].length)) {
					return
				}
                fetchTrainStation(data).then(res => {
                    const {code, data, msg} = res
                    if (code === 1) {
                        const {jiang = [], hot = [], all = []} = data
                        commit('setTrainStation', {jiang, hot, all})
						commit('setStationType', type)
                    } else {
                        console.error(msg)
                    }
                })
            } else {
				if (Object.keys(airStation).some(e => airStation[e].length)) {
					return
				}
                fetchAirPortStation(data).then(res => {
                    const {code, data, msg} = res
                    if (code === 1) {
                        const {jiang = [], hot = [], all = []} = data
                        commit('setAirPort', {jiang, hot, all})
                    } else {
                        console.error(msg)
                    }
                })
            }
        }
    }
}
